<template>
  <div id="app">
    <header class="app-header">
      <div class="logo-container">
        <router-link to="/" class="logo">
          <img src="@/assets/logo.png" alt="乐谱智识logo" class="logo-image">
          <span class="logo-text">乐谱智识-音乐教育平台</span>
        </router-link>
      </div>
      
      <nav class="main-nav">
        <router-link to="/" class="nav-link" exact>首页</router-link>
        <router-link to="/scanner" class="nav-link">乐谱识别</router-link>
        <router-link to="/chat" class="nav-link" :class="{ 'active': $route.path.startsWith('/chat') }">音乐助手</router-link>
        <router-link to="/community" class="nav-link">音乐社区</router-link>
      </nav>
      
      <div class="user-nav">
        <div class="theme-toggle">
          <button @click="toggleTheme" class="theme-toggle-btn" :title="isDarkMode ? '切换到亮色模式' : '切换到暗色模式'">
            <i class="fas" :class="isDarkMode ? 'fa-sun' : 'fa-moon'"></i>
          </button>
        </div>
        <template v-if="isAuthenticated">
          <router-link to="/profile" class="user-profile-link">
            <div class="avatar">
              <img :src="avatarUrl" alt="用户头像">
            </div>
            <span class="username">{{ username }}</span>
          </router-link>
          <button @click="logout" class="logout-btn">退出</button>
        </template>
        <template v-else>
          <router-link to="/login" class="login-btn">登录</router-link>
          <router-link to="/register" class="register-btn">注册</router-link>
        </template>
      </div>
    </header>

    <main class="main-content">
      <div class="router-view-container page-transition">
        <transition
          :name="transitionName"
          @before-leave="beforeLeave"
          @enter="enter"
          @after-enter="afterEnter"
          mode="out-in"
        >
          <router-view :key="$route.fullPath" class="page-transition"></router-view>
        </transition>
      </div>
    </main>

    <footer class="app-footer">
      <p>&copy; 2025 乐谱智识-音乐教育平台 保留所有权利</p>
    </footer>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      isAuthenticated: false,
      username: '',
      avatarUrl: require('@/assets/logo.png'), // 默认头像改为使用logo
      transitionName: 'apple-slide', // 默认使用苹果风格滑动
      lastScrollPosition: 0,
      pageTransitionActive: false,
      isDarkMode: false
    };
  },
  created() {
    // 加载动画库
    const animateCss = document.createElement('link');
    animateCss.rel = 'stylesheet';
    animateCss.href = 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css';
    document.head.appendChild(animateCss);
    
    // 检查本地存储中是否有登录信息
    this.refreshUserInfo();
    
    // 检查本地存储的主题设置
    const savedTheme = localStorage.getItem('theme');
    this.isDarkMode = savedTheme === 'dark';
    this.applyTheme();
    
    // 监听登录和注册成功事件
    window.addEventListener('user-login-success', this.refreshUserInfo);
    window.addEventListener('user-register-success', this.refreshUserInfo);
    // 监听头像更新事件
    window.addEventListener('user-avatar-updated', this.refreshUserInfo);
    
    // 监听路由变化，决定动画效果
    this.$router.beforeEach((to, from, next) => {
      // 标记当前正在进行页面过渡
      this.pageTransitionActive = true;
      
      // 优先使用路由定义中的过渡效果
      if (to.meta && to.meta.transition) {
        this.transitionName = to.meta.transition;
      } else {
        // 根据导航深度决定过渡方向
        const toDepth = to.path.split('/').length;
        const fromDepth = from.path.split('/').length;
        
        if (fromDepth < toDepth) {
          this.transitionName = 'slide-left';
        } else if (fromDepth > toDepth) {
          this.transitionName = 'slide-right';
        } else {
          this.transitionName = 'fade';
        }
      }
      
      next();
    });
  },
  beforeUnmount() {
    // 移除事件监听器
    window.removeEventListener('user-login-success', this.refreshUserInfo);
    window.removeEventListener('user-register-success', this.refreshUserInfo);
    window.removeEventListener('user-avatar-updated', this.refreshUserInfo);
  },
  methods: {
    refreshUserInfo() {
      // 检查本地存储中是否有登录信息
      const userInfo = localStorage.getItem('user');
      let token = localStorage.getItem('token'); 
      
      console.log('刷新用户信息:', {userInfo, token});
      
      // 解析用户信息
      let user = null;
      if (userInfo) {
        try {
          user = JSON.parse(userInfo);
          // 如果用户对象内有token/access_token，优先使用它
          if (user.access_token && !token) {
            token = user.access_token;
          } else if (user.token && !token) {
            token = user.token;
          }
        } catch (e) {
          console.error('解析用户信息失败:', e);
        }
      }
      
      if (user && token) {
        this.isAuthenticated = true;
        this.username = user.name || user.username || '用户';
        
        // 如果用户有自定义头像，则使用自定义头像
        if (user.avatar_url || user.avatar) {
          this.avatarUrl = user.avatar_url || user.avatar;
        } else {
          this.avatarUrl = require('@/assets/logo.png'); // 默认头像改为使用logo
        }
        
        console.log('用户已登录:', this.username, this.avatarUrl);
      } else {
        this.isAuthenticated = false;
        this.username = '';
        this.avatarUrl = require('@/assets/logo.png'); // 默认头像改为使用logo
        console.log('用户未登录');
        
        // 清除无效的存储
        if (userInfo && !token) {
          localStorage.removeItem('user');
        }
        if (!userInfo && token) {
          localStorage.removeItem('token');
        }
      }
    },
    
    logout() {
      // 清除本地存储并重置状态
      localStorage.removeItem('user');
      localStorage.removeItem('token'); // 注意这里使用'token'而不是'access_token'
      this.isAuthenticated = false;
      this.username = '';
      this.avatarUrl = require('@/assets/logo.png'); // 默认头像改为使用logo
      
      // 重定向到首页
      this.$router.push('/');
    },
    
    beforeLeave(element) {
      // 记住离开前的滚动位置
      this.lastScrollPosition = window.scrollY;
      
      // 确保离开动画流畅
      requestAnimationFrame(() => {
        element.style.willChange = 'transform, opacity';
        element.style.position = 'absolute';
        element.style.width = '100%';
        element.style.top = '0';
        element.style.left = '0';
        element.style.zIndex = '1';
      });
    },
    
    enter(element) {
      // 确保新页面从顶部开始
      window.scrollTo(0, 0);
      
      // 确保进入动画流畅
      requestAnimationFrame(() => {
        element.style.willChange = 'transform, opacity';
        element.style.position = 'relative';
        element.style.zIndex = '2';
      });
    },
    
    afterEnter() {
      // 页面转换完成
      this.pageTransitionActive = false;
      
      // 清除页面过渡锁定
      document.documentElement.classList.remove('page-transitioning');
    },
    
    toggleTheme() {
      this.isDarkMode = !this.isDarkMode;
      // 保存到本地存储
      localStorage.setItem('theme', this.isDarkMode ? 'dark' : 'light');
      this.applyTheme();
    },
    
    applyTheme() {
      // 应用主题样式
      if (this.isDarkMode) {
        document.documentElement.classList.add('dark-theme');
      } else {
        document.documentElement.classList.remove('dark-theme');
      }
    }
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  scrollbar-width: none; /* Firefox */
}

:root {
  --background-color: #f9faff;
  --text-color: #333;
  --header-bg: rgba(255, 255, 255, 0.95);
  --card-bg: #fff;
  --border-color: #e0e7ff;
  --shadow-color: rgba(79, 70, 229, 0.08);
  --primary-color: #4361ee;
  --secondary-color: #3a0ca3;
  --accent-color: #4f46e5;
  --hover-bg: rgba(67, 97, 238, 0.05);
  --footer-bg: #f3f4ff;
  --btn-text: #555;
  --divider-color: #f0f0f0;
  --transition: all 0.3s ease;
  
  /* 输入控件 */
  --input-bg: #fff;
  --input-border: #ddd;
  --input-text: #333;
  --input-placeholder: #888;
  --input-focus-border: #4f46e5;
  --input-focus-shadow: rgba(79, 70, 229, 0.2);
  
  /* 按钮 */
  --button-primary-bg: #4361ee;
  --button-primary-text: #fff;
  --button-secondary-bg: #f5f5f5;
  --button-secondary-text: #555;
  --button-hover-transform: translateY(-2px);
  
  /* 卡片和模态框 */
  --card-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  --modal-bg: rgba(0, 0, 0, 0.75);
  --modal-backdrop: blur(3px);
  
  /* 特殊组件 */
  --sidebar-bg: #f5f7ff;
  --message-sent-bg: linear-gradient(135deg, #4361ee, #3a0ca3);
  --message-received-bg: #f5f7ff;
  --message-received-text: #333;
  
  /* 表格和列表 */
  --table-header-bg: #f5f7ff;
  --table-row-hover: #f0f4ff;
  --list-item-hover: #eef1ff;
  --list-item-active: #e6eaff;
  
  /* 滚动条 */
  --scrollbar-track: #f5f5f5;
  --scrollbar-thumb: #d0d0d0;
}

.dark-theme {
  --background-color: #121212;
  --text-color: #ffffff;
  --header-bg: rgba(18, 18, 18, 0.95);
  --card-bg: #1e1e24;
  --border-color: #2a2a35;
  --shadow-color: rgba(0, 0, 0, 0.3);
  --primary-color: #818cf8;
  --secondary-color: #a78bfa;
  --accent-color: #6366f1;
  --hover-bg: rgba(99, 102, 241, 0.1);
  --footer-bg: #121212;
  --btn-text: #ffffff;
  --divider-color: #2a2a35;
  
  /* 输入控件 */
  --input-bg: #252530;
  --input-border: #3a3a45;
  --input-text: #ffffff;
  --input-placeholder: #aaaaaa;
  --input-focus-border: #6366f1;
  --input-focus-shadow: rgba(99, 102, 241, 0.2);
  
  /* 按钮 */
  --button-primary-bg: #6366f1;
  --button-primary-text: #fff;
  --button-secondary-bg: #2a2a35;
  --button-secondary-text: #ffffff;
  
  /* 卡片和模态框 */
  --card-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
  --modal-bg: rgba(0, 0, 0, 0.85);
  
  /* 特殊组件 */
  --sidebar-bg: #15151a;
  --message-sent-bg: linear-gradient(135deg, #4338ca, #5b21b6);
  --message-received-bg: #252530;
  --message-received-text: #ffffff;
  
  /* 表格和列表 */
  --table-header-bg: #252530;
  --table-row-hover: #2a2a35;
  --list-item-hover: #25252f;
  --list-item-active: #2a2a40;
  
  /* 滚动条 */
  --scrollbar-track: #252530;
  --scrollbar-thumb: #4a4a55;
}

*::-webkit-scrollbar {
  display: none; /* Chrome, Safari, Edge */
}

body {
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
  line-height: 1.6;
  color: var(--text-color);
  background-color: var(--background-color);
  -ms-overflow-style: none; /* IE and Edge */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: var(--transition);
  margin: 0;
  padding: 0;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.app-header {
  background-color: var(--header-bg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 2px 20px var(--shadow-color);
  padding: 0.8rem 2.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
  transition: var(--transition);
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: var(--primary-color);
  font-weight: 700;
  font-size: 1.2rem;
  transition: transform 0.3s ease;
}

.logo:hover {
  transform: scale(1.02);
}

.logo-image {
  height: 42px;
  margin-right: 12px;
  filter: drop-shadow(0 2px 5px rgba(67, 97, 238, 0.3));
}

.logo-text {
  font-size: 1.3rem;
  background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}

.main-nav {
  display: flex;
  gap: 2rem;
}

.nav-link {
  color: var(--btn-text);
  text-decoration: none;
  font-weight: 500;
  padding: 0.5rem 0.25rem;
  position: relative;
  transition: var(--transition);
}

.nav-link:hover {
  color: var(--primary-color);
}

.nav-link.router-link-active, .nav-link.active {
  color: var(--primary-color);
  font-weight: 600;
}

.nav-link.router-link-active:after, .nav-link.active:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
  border-radius: 2px;
  box-shadow: 0 1px 3px var(--shadow-color);
}

.user-nav {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.theme-toggle {
  display: flex;
  align-items: center;
}

.theme-toggle-btn {
  background: none;
  border: none;
  font-size: 1.2rem;
  color: #555;
  cursor: pointer;
  transition: color 0.3s;
}

.theme-toggle-btn:hover {
  color: #4361ee;
}

.user-profile-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  padding: 0.4rem;
  border-radius: 8px;
  transition: all 0.3s;
}

.user-profile-link:hover {
  background-color: rgba(67, 97, 238, 0.05);
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 8px;
  border: 2px solid #e0e7ff;
  box-shadow: 0 2px 8px rgba(67, 97, 238, 0.15);
  transition: all 0.3s;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-profile-link:hover .avatar {
  transform: scale(1.05);
  border-color: #4361ee;
  box-shadow: 0 3px 12px rgba(67, 97, 238, 0.25);
}

.username {
  font-weight: 500;
  color: #4361ee;
}

.logout-btn {
  background: none;
  border: 1px solid #e0e7ff;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  color: #555;
  transition: all 0.3s;
}

.logout-btn:hover {
  background-color: #f0f4ff;
  color: #4361ee;
  box-shadow: 0 2px 8px rgba(67, 97, 238, 0.1);
}

.login-btn, .register-btn {
  text-decoration: none;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  transition: all 0.3s;
  font-weight: 500;
}

.login-btn {
  color: #4361ee;
  border: 1px solid #4361ee;
  background-color: rgba(67, 97, 238, 0.05);
}

.login-btn:hover {
  background-color: rgba(67, 97, 238, 0.1);
  box-shadow: 0 2px 10px rgba(67, 97, 238, 0.15);
}

.register-btn {
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  color: white;
  box-shadow: 0 3px 10px rgba(67, 97, 238, 0.2);
}

.register-btn:hover {
  background: linear-gradient(135deg, #3a56e8, #3407a1);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(67, 97, 238, 0.25);
}

.main-content {
  flex: 1;
  padding-bottom: 2rem;
}

.app-footer {
  background-color: var(--footer-bg);
  padding: 2rem;
  text-align: center;
  border-top: 1px solid var(--border-color);
  margin-top: 2rem;
}

.app-footer p {
  color: var(--btn-text);
  font-size: 0.9rem;
  font-weight: 500;
}

/* 页面过渡动画 - 优化版 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 400ms cubic-bezier(0.5, 0, 0.3, 1), transform 400ms cubic-bezier(0.5, 0, 0.3, 1);
  will-change: transform, opacity;
}

.fade-enter-from {
  opacity: 0;
  transform: translateY(10px);
}

.fade-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}

.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active {
  transition: transform 500ms cubic-bezier(0.5, 0, 0.3, 1), opacity 500ms cubic-bezier(0.5, 0, 0.3, 1);
  will-change: transform, opacity;
  position: absolute;
  width: 100%;
}

.slide-left-enter-from {
  opacity: 0;
  transform: translateX(30px);
}

.slide-left-leave-to {
  opacity: 0;
  transform: translateX(-30px);
}

.slide-right-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}

.slide-right-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

.scale-enter-active,
.scale-leave-active {
  transition: all 500ms cubic-bezier(0.5, 0, 0.3, 1);
  will-change: transform, opacity;
}

.scale-enter-from {
  opacity: 0;
  transform: scale(0.95);
}

.scale-leave-to {
  opacity: 0;
  transform: scale(1.05);
}

/* 响应式样式 - 增强版 */
/* 手机端样式 */
@media (max-width: 768px) {
  .app-header {
    flex-direction: column;
    padding: 1rem 1.5rem;
  }
  
  .logo-container {
    margin-bottom: 0.8rem;
  }
  
  .main-nav {
    width: 100%;
    justify-content: space-between;
    margin: 0.8rem 0;
    gap: 1rem;
    overflow-x: auto;
    padding-bottom: 5px;
  }
  
  .nav-link {
    white-space: nowrap;
    padding: 0.5rem 0.8rem;
  }
  
  .user-nav {
    width: 100%;
    justify-content: center;
    margin-top: 0.8rem;
  }
  
  .logo-text {
    font-size: 1.1rem;
  }
  
  .main-content {
    padding-top: 1rem;
  }
}

/* 平板样式 */
@media (min-width: 769px) and (max-width: 1024px) {
  .app-header {
    padding: 1rem 2rem;
  }
  
  .main-nav {
    gap: 1.5rem;
  }
  
  .logo-text {
    font-size: 1.2rem;
  }
}

/* 大屏幕样式 */
@media (min-width: 1440px) {
  .app-header {
    padding: 1.2rem 4rem;
  }
  
  .main-nav {
    gap: 3rem;
  }
  
  .nav-link {
    font-size: 1.1rem;
  }
  
  .logo-image {
    height: 50px;
  }
  
  .logo-text {
    font-size: 1.5rem;
  }
  
  .main-content {
    padding-bottom: 3rem;
  }
  
  .app-footer {
    padding: 2.5rem;
  }
  
  .app-footer p {
    font-size: 1rem;
  }
}

/* 4K显示器样式 */
@media (min-width: 2560px) {
  .app-header {
    padding: 1.5rem 6rem;
  }
  
  .main-nav {
    gap: 4rem;
  }
  
  .nav-link {
    font-size: 1.3rem;
    padding: 0.8rem 0.4rem;
  }
  
  .nav-link.router-link-active:after {
    height: 3px;
  }
  
  .logo-image {
    height: 60px;
    margin-right: 16px;
  }
  
  .logo-text {
    font-size: 1.8rem;
  }
  
  .user-nav {
    gap: 1.5rem;
  }
  
  .avatar {
    width: 45px;
    height: 45px;
  }
  
  .username {
    font-size: 1.1rem;
  }
  
  .login-btn, .register-btn, .logout-btn {
    font-size: 1.1rem;
    padding: 0.6rem 1.3rem;
  }
  
  .main-content {
    padding-bottom: 4rem;
  }
  
  .app-footer {
    padding: 3rem;
  }
  
  .app-footer p {
    font-size: 1.2rem;
  }
  
  /* 增大过渡动画效果 */
  .fade-enter-from, .fade-leave-to,
  .slide-left-enter-from, .slide-left-leave-to,
  .slide-right-enter-from, .slide-right-leave-to,
  .scale-enter-from, .scale-leave-to {
    transform: translateY(20px);
  }
}

.app-container {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style> 